package com.totoro.mapstruct.mapper;

import com.totoro.mapstruct.bean.Car;
import com.totoro.mapstruct.bean.CarDto;
import org.mapstruct.*;
import org.mapstruct.factory.Mappers;

/**
 * Created by Niki on 2018/8/8 9:28
 */
@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
public interface CarMapper {
    CarMapper instance = Mappers.getMapper(CarMapper.class);

    @Mappings({
            @Mapping(source = "manufacturingDate", target = "data", dateFormat = "yyyy-MM-dd"),
            @Mapping(source = "type.name", target = "type"),
            @Mapping(source = "numberOfSeats", target = "seatCount"),
    })
    CarDto carToDto(Car car);

    @Mappings({
            @Mapping(target = "manufacturingDate", source = "data", dateFormat = "yyyy-MM-dd"),
            @Mapping(target = "type.name", source = "type"),
            @Mapping(target = "numberOfSeats", source = "seatCount")
    })
    Car dtoToCar(CarDto dto);

    /**
     * 修改Bean，并不是根据源bean生成目标bean
     * 当DTO中对象为null 如果需要不修改，则需要修改Mapper的NullValueCheckStrategy策略为always
     * @param carDto
     * @param car
     */
    @Mappings(value = {
            @Mapping(target = "type.name", source = "type"),
            @Mapping(target = "numberOfSeats", source = "seatCount"),
    })
    void updateCarFromDto(CarDto carDto, @MappingTarget Car car);
}
